﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using ForumBL;

namespace Forum.ValidationAttributes
{
    public class IsUsernameInDatabase : ValidationAttribute
    {
        private string errorMessage;

        public IsUsernameInDatabase()
        {
        }

        public IsUsernameInDatabase(string errMessage)
        {
            this.errorMessage = errMessage;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            bool isValid = IsUsernameBusy(value.ToString());
            if (isValid)
            {
                return new ValidationResult(errorMessage);
            }
            else
            {
                return ValidationResult.Success;
            }
        }

        private bool IsUsernameBusy(String username)
        {
            UserBL userContext = new UserBL();
            bool isUsernameBusy = userContext.IsUsernameBusy(username);
            return isUsernameBusy;
        }
    }
}